package com.study.example.gameserver;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.core.util.StatusPrinter;
import com.sparrow.common.util.MysqlUtil;
import com.sparrow.event.api.EventBus;
import com.sparrow.common.util.PropertiesUtil;
import com.sparrow.event.model.Event;
import com.study.example.gameserver.event.ServerInitEvent;
import com.study.example.gameserver.log.SystemLogType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.io.File;
import java.util.concurrent.TimeUnit;

/**
 * 游戏服启动类
 */
public class GameBootstrap {

    private static final Logger logger = LoggerFactory.getLogger(GameBootstrap.class);

    public static void main(String[] args) {
        LoggerContext lc = (LoggerContext) org.slf4j.LoggerFactory.getILoggerFactory();
        StatusPrinter.print(lc);
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (Exception e) {

        }
        PropertiesUtil.load("server/database.properties", "server/server.properties");
        MysqlUtil.tryCreateDBIfNotExist();

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(GameConfig.class);
        context.refresh();

        EventBus eventBus = context.getBean(EventBus.class);
        eventBus.publish(Event.newBuilder(new ServerInitEvent()).sync().build());

        Runtime.getRuntime().addShutdownHook(new GameShutDownHandler(context));
        logger.info("游戏服启动成功");
        SystemLogType.system.info("server started");
    }

}
